package com.org.khtn.gui;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JPopupMenu;
import javax.swing.JTree;
import javax.swing.tree.TreePath;


public class TreePopupListener extends MouseAdapter {
	private static JTree treeExplorer;
	private JPopupMenu databasesPopupMenu;
	private JPopupMenu userDatabasePopupMenu;
	private JPopupMenu tablesPopupMenu;
	private JPopupMenu userTablePopupMenu;
	
	public TreePopupListener(JTree tree, JPopupMenu databases, JPopupMenu userDatabase, JPopupMenu tables, JPopupMenu userTable)
	{
		treeExplorer = tree;
		databasesPopupMenu = databases;
		userDatabasePopupMenu = userDatabase;
		tablesPopupMenu = tables;
		userTablePopupMenu = userTable;
	}

    public void mousePressed(MouseEvent e) {
        maybeShowPopup(e);
    }

    public void mouseReleased(MouseEvent e) {
        maybeShowPopup(e);
    }

    private void maybeShowPopup(MouseEvent e) {
        if (e.isPopupTrigger()) {
            TreePath path = treeExplorer.getPathForLocation(e.getX(), e.getY());
            treeExplorer.setSelectionPath(path);
            /*if (path != null) {
                if ((path.getLastPathComponent().toString().compareToIgnoreCase("Databases") == 0)
                        && (path.getParentPath().getLastPathComponent().toString().compareToIgnoreCase("Server") == 0)
                        && (path.getPathCount() == 2)) {
                    databasesPopupMenu.show(e.getComponent(), e.getX(), e.getY());
                } else if ((path.getParentPath().getLastPathComponent().toString().compareToIgnoreCase("Databases") == 0)
                        && (path.getPathCount() == 3)) {
                    userDatabasePopupMenu.show(e.getComponent(), e.getX(), e.getY());
                } else if ((path.getLastPathComponent().toString().compareToIgnoreCase("Tables") == 0)
                        && (path.getPathCount() == 4)) {
                    tablesPopupMenu.show(e.getComponent(), e.getX(), e.getY());
                } else if ((path.getParentPath().getLastPathComponent().toString().compareToIgnoreCase("Tables") == 0)
                        && (path.getPathCount() == 5)) {
                    userTablePopupMenu.show(e.getComponent(), e.getX(), e.getY());
                }
            }*/
            
            if (path != null) {
                if (path.getPathCount() == 2) {
                    databasesPopupMenu.show(e.getComponent(), e.getX(), e.getY());
                } else if (path.getPathCount() == 3) {
                    userDatabasePopupMenu.show(e.getComponent(), e.getX(), e.getY());
                } else if (path.getPathCount() == 4) {
                    tablesPopupMenu.show(e.getComponent(), e.getX(), e.getY());
                } else if (path.getPathCount() == 5) {
                    userTablePopupMenu.show(e.getComponent(), e.getX(), e.getY());
                }
            }
        }
    }
}